<?php
/**
 * Created by PhpStorm.
 * User: luzy
 * Date: 2018/5/16
 * Time: 下午3:27
 */

class Salesmanproducttext extends Base_Controller
{
    public function __construct()
    {
        $this->_page_size = 20;
        $this->_form_type = 'salesman_product_text';
        $this->_list_where = array('type'=>2,'gid'=>0);//文案
        $this->_action_list = array('EDIT');//EDIT,DEL,array('controller'=>'admin/commanswer/','function'=>'showlist','label'=>'查看回答')
        $this->_controller = 'admin/salesman/salesmanproducttext/';//admin/user/
        parent::__construct();
    }

    public function showList($gid = 0,$page = 1)
    {
        $this->_external_id = $gid;
        $this->_list_where['gid']=$gid;
        $limit = $this->_page_size;
        $offset = ($page-1) * $limit;
        $pdata = $this->getPagination($this->_controller.'showList/'.$this->_external_id.'/');

        $res = $this->getList($offset,$limit);
        $this->load->model('kdtsalesman_model');
        foreach ($res['list'] as &$row){
            $row['gid']=$this->kdtsalesman_model->getGoodsnameByGid($row['gid']);
            $row['status']=$row['status']==1?'显示':'隐藏';
        }//处理表数据
        //有创建按钮
        $createbtn = $this->load->view('admin/form/createbtn',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id),true);
        //有返回按钮
        $backbtn = $this->load->view('admin/form/backbtn',array('controller'=>site_url('admin/salesman/salesmanproduct/')),true);

        $data = array('headers'=>$res['headers'],'list'=>$res['list'],'pagination'=>$pdata['pagination'],'createbtn'=>$createbtn,'backbtn'=>$backbtn);
        $content = $this->load->view('admin/form/list',$data,true);

        echo $this->getAdminView($content);
    }

    public function update($id = 0, $gid = 0)
    {
        $this->_external_id = $gid;
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->updateBuildForm($id);
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['type']=2;
            $post['gid']=$gid;
            $this->load->model('kdtsalesman_model');
            $post['content']=$this->kdtsalesman_model->text2br($post['content']);
            $post['status']=(int)$post['status'];
            $post['sort']=(int)trim($post['sort']);
            $this->_update_data = $post;
            parent::update($id);
        }
    }
    public function afterupdate($pkey,$pkeyval){}

    public function create($gid = 0)
    {
        $this->_external_id = $gid;
        $post = $this->input->post();
        if(empty($post)){
            $form = $this->createBuildForm();
            $content = $this->load->view('admin/form/edit',array('controller'=>site_url($this->_controller),'param'=>$this->_external_id,'form'=>$form),true);
            echo $this->getAdminView($content);
        }else{
            /******待编辑******/
            //filter post data
            $post['type']=2;
            $post['gid']=$gid;
            $this->load->model('kdtsalesman_model');
            $post['content']=$this->kdtsalesman_model->text2br($post['content']);
            $post['status']=(int)$post['status'];
            $post['sort']=(int)trim($post['sort']);
            $this->_create_data = $post;
            parent::create();
        }
    }

    public function aftercreate($pkeyval)
    {
        /*$pkey为主键，$pkeyval为主键值*/
    }

    public function delete($id = 0,$externalid = 0)
    {
        $this->_external_id = $externalid;
        parent::delete($id);
    }

    public function afterdelete($pkey,$pkeyval){}
}